<template>
  <div class="shopcar-container">
    <div class="goods-list">
      <div class="mui-card" v-for="item in goodslist" :key="item.id">
        <div class="mui-card-content">
          <div class="mui-card-content-inner goods-item">
            <mt-switch v-model="selectedObj[item.id]" @change="changeSelect(item.id,selectedObj[item.id])"></mt-switch>
            <img src="../../images/iPhone12.jpg" />
            <div class="info">
              <h1>{{item.title}}</h1>
              <nobox :initcount="countObj[item.id]" :id="item.id"></nobox>
              <div class="goods-info">
                <span class="price">￥{{item.sell_price}}</span>
                <a href="#" @click.prevent="del(item.id)">删除</a>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="mui-card">
        <div class="mui-card-content">
          <div class="mui-card-content-inner jiesuan">
            <div class="left">
              <h3>总计(不含运费)</h3>
              <p>
                已勾选商品
                <span class="danger">{{selectedcount}}</span>件，总价
                <span class="danger">￥{{pricecount}}</span>
              </p>
            </div>
            <mt-button type="danger">去结算</mt-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
 

<script>
import nobox from "../sub-components/shopcar_nobox.vue";
import { mapGetters,mapMutations } from "vuex";
export default {
  data() {
    return {
      goodslist: []
    };
  },
  methods: {
    ...mapMutations(["delFromCart","changeSelectState"]),
    getGoodsList() {
      if(this.idstr.length<=0) return ;
      this.$http.get("/api/goods/getshopcarlist/" + this.idstr).then(result => {
        this.goodslist = result.data.message;
      });
    },
    del(id){
      this.goodslist.some((item,i)=>{
        if(item.id==id){
          this.goodslist.splice(i,1);
          return true;
        }
      });  
      this.delFromCart(id)
    },
    changeSelect(id,select){
      this.changeSelectState({id:id,selected:select})
    }    
  },
  components: {
    nobox
  },
  computed: {
    ...mapGetters(["idstr","countObj","selectedObj","selectedcount","pricecount"]),
    
  },
  
  created() {
    this.getGoodsList();
  }
};
</script>
<style lang="scss" scoped>
.shopcar-container {
  background-color: #eee;
  overflow: hidden;

  .goods-item {
    display: flex;
    img {
      width: 60px;
      height: 65px;
    }
    h1 {
      font-size: 13px;
    }
    .info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .goods-info {
        display: flex;
        justify-content: space-between;

        .price {
          color: red;
          font-size: 16px;
          font-weight: bold;
        }
      }
    }
  }
}
.danger {
  color: red;
  font-size: 16px;
  font-weight: bold;
}
.jiesuan {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
